<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "image".
 *
 * @property int $id id
 * @property string $image 图片地址
 * @property int $is_cdn 已储存到cdn
 * @property int $created_at 创建时间
 * @property int $type 类型
 * @property int $status 状态
 * @property string $alt 描述
 *
 * @property Goods[] $goods
 * @property JobInfo[] $jobInfos
 * @property UserCertification[] $userCertifications
 */
class Image extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'image';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['is_cdn', 'created_at', 'type', 'status'], 'integer'],
            [['image', 'alt'], 'string', 'max' => 255],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => 'id',
            'image' => '图片地址',
            'is_cdn' => '已储存到cdn',
            'created_at' => '创建时间',
            'type' => '类型',
            'status' => '状态',
            'alt' => '描述',
        ];
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getGoods()
    {
        return $this->hasMany(Goods::className(), ['img' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getJobInfos()
    {
        return $this->hasMany(JobInfo::className(), ['img' => 'id']);
    }

    /**
     * @return \yii\db\ActiveQuery
     */
    public function getUserCertifications()
    {
        return $this->hasMany(UserCertification::className(), ['img' => 'id']);
    }

    public function getImageUrl()
    {
        $cdn       = Yii::$app->get('cdn');
        $cdn->host = Yii::$app->feehi->website_url;

        return $cdn->getCdnUrl($this->image);
    }

}
